import { Gentle } from '@/common/tools/themeColor';
import { getEnumValue } from '@/common/SDK/utils';

function getSetColor(enums:any,key:string = 'SLIDER_COLOR'):unknown{
    if(!window.localStorage.getItem('setting-theme')){
        return '#' + getEnumValue(enums,key)
    } else {
       let colorObj:null|any = JSON.parse(<string>window.localStorage.getItem('setting-theme'));
       return colorObj[key]
    }
}

let state = {
    //主题颜色
    themeColor: {
        //设置侧边栏样式
        SLIDER_COLOR: getSetColor(Gentle,'SLIDER_COLOR'),
        //背景颜色
        MAIN_COLOR: getSetColor(Gentle,'MAIN_COLOR'),
        /*按钮颜色*/
        BUTTON_XL_COLOR: getSetColor(Gentle,'BUTTON_XL_COLOR')
    }
}

let mutations = {
    /*改变主题*/
    SET_THEME_COLOR(state:any,value:any){
        state.themeColor.SLIDER_COLOR = '#' + value.SLIDER_COLOR;
        state.themeColor.MAIN_COLOR = '#' + value.MAIN_COLOR;
        state.themeColor.BUTTON_XL_COLOR = '#' + value.BUTTON_XL_COLOR;
        window.localStorage.setItem('setting-theme',JSON.stringify(Object.assign(state.themeColor)))
    },
}

const actions = {
    changeThemeColor({ commit }:any,enums:any){
        commit('SET_THEME_COLOR',enums)
    }
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}
